/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.ucm;

import assignment_cp3120.user.deleteUser.DeleteUserModel;
import assignment_cp3120.user.deleteUser.DeleteUserView;
import assignment_cp3120.user.UserView;

/**
 *  DELETE USER USE-CASE
 * 
 * Precondition: System has shown password verification screen (DeleteUserView)
 *<P>
 *  1. User enters username and password
 *  2. User clicks delete
 *  3. System validates username and password 
 *  4. System validates that the user is a user
 *  5. System deletes user from database 
 *  6. System shows welcome screen
 * 
 *  3.a.1 System detects that the username does not exist in database
 *  3.a.2 System shows error dialog
 *  3.a.3 System clears username and password fields
 *<P> 
 *  4.a.1 System detects that the user is not a user
 *  4.a.2 System shows error dialog
 *  4.a.3 System hides the password verification screen
 *<P>
 *  Postcondition: Display is updated accordingly 
 * @author Richard and Tristan
 */
public class UCDeleteUser implements UseCaseController {
    private DeleteUserModel model;
    private DeleteUserView view;

    /**
     * Defualt constructor, calls Constructor(model,view) with new model and new view
     */
    public UCDeleteUser(){
        this(new DeleteUserModel(), DeleteUserView.getInstance());
    }
    /**
     * Constructor that sets the model, view and frame to the input valus and sets the buttonlisteners
     * @param model
     * Set this.model to the input
     * @param view
     * Set this.view to the input
     */
    public UCDeleteUser(DeleteUserModel model, DeleteUserView view) {
        this.model = model;
        this.view = view;
    }
    /**
     * Run fuction to start the use case. Shows the GUI
     */
    public void run() {        
        //3. System validates username and password
        if(model.validate(view.getUsernameField(),new String(view.getPasswordField()))){
            //4. System validates that the user is a user
            if (model.userIsUser()){
                //5. System deletes from database 
                model.deleteUser();
                view.successMessageBox();
                view.hideGUI();
                //6. System shows welcome screen
                UserView.getInstance().logout();
            }
            //4.a.1 System detects that the user is not a user
            else {                
                //4.a.2 System shows error dialog
                view.notUserMessageBox();
                //4.a.3 System hides the password verification screen)
                view.hideGUI();
            }
        }
        //3.a.1 System detects that the username does not exist in database
        else{
            //3.2 System shows error dialog
            view.userPassErrorMessageBox();
            //3.a.3 System clears username and password fields 
            view.clearUsernamePasswordField();
        }
    }
}
